package com.whfc.ms.api.emp.controller;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.service.ClassMeetingInspectionService;
import com.whfc.fuum.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 班会签到巡检控制器
 * @Author dzg
 * @Date 2025-11-10
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/ms/api/classMeeting/inspection")
public class ClassMeetingInspectionController {

    @DubboReference(interfaceClass = ClassMeetingInspectionService.class, version = "1.0.0", timeout = 30000)
    private ClassMeetingInspectionService classMeetingInspectionService;

    /**
     * 执行班会签到巡检
     * 检测当天已开始或结束的班会中是否有未签到人员，若存在则发送消息给旁站监护人员
     *
     * @param user 当前登录用户
     * @return 巡检结果
     */
//    @RequiresPermissions("emp/classMeeting/inspection/execute")
    @PostMapping("/execute")
    public Result executeInspection(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        log.info("用户 {} 手动触发班会签到巡检", user.getUsername());
        
        try {
            classMeetingInspectionService.executeInspection();
            return ResultUtil.success("班会签到巡检执行成功");
        } catch (BizException e) {
            log.error("班会签到巡检执行失败", e);
            return ResultUtil.failure(e.getMessage());
        } catch (Exception e) {
            log.error("班会签到巡检执行异常", e);
            return ResultUtil.failure("巡检执行异常，请稍后重试");
        }
    }

    /**
     * 执行指定班会的签到巡检
     *
     * @param user 当前登录用户
     * @param meetingId 班会ID
     * @return 巡检结果
     */
//    @RequiresPermissions("emp/classMeeting/inspection/execute")
    @PostMapping("/execute/{meetingId}")
    public Result executeInspectionByMeetingId(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                             @PathVariable("meetingId") Integer meetingId) {
        log.info("用户 {} 手动触发班会签到巡检，班会ID: {}", user.getUsername(), meetingId);
        
        try {
            classMeetingInspectionService.executeInspectionByMeetingId(meetingId);
            return ResultUtil.success("班会签到巡检执行成功");
        } catch (BizException e) {
            log.error("班会签到巡检执行失败，班会ID: {}", meetingId, e);
            return ResultUtil.failure(e.getMessage());
        } catch (Exception e) {
            log.error("班会签到巡检执行异常，班会ID: {}", meetingId, e);
            return ResultUtil.failure("巡检执行异常，请稍后重试");
        }
    }

    /**
     * 获取班会签到巡检状态
     *
     * @param user 当前登录用户
     * @return 巡检状态信息
     */
//    @RequiresPermissions("emp/classMeeting/inspection/status")
    @GetMapping("/status")
    public Result getInspectionStatus(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        log.info("用户 {} 查询班会签到巡检状态", user.getUsername());
        
        // 这里可以返回巡检的统计信息、最近执行时间等
        // 暂时返回简单状态信息
        return ResultUtil.success("班会签到巡检服务运行正常");
    }
}